<?php
	require_once(dirname(__FILE__)."/../core/safeParam.php");
	require_once '../core/functions.php';
		
	global $database;
	$vars 	= array();
	$rating 	= array();
	$options = array();
	$error 	= 0;
	$msgError = '';
	
	$usuario = $_POST['usuario'];
	$permisosUsuario = $usuario;
	$permisosUsuario .= '_authorized';
	
	$vars['sitename']		= $_POST['nombre'];
	$siteNameAux = str_replace(" ", "_", $vars['sitename']);
	$vars['sitename'] = $siteNameAux;
	
	$vars['description']	= $_POST['descripcion'];
	
	if ($_FILES['logo']['size'] != 0) {
		$image = uploadImage($_FILES, '1200', '1200', '../UserFiles/Image/product_photos/', 'logo');
     	$vars['logo'] = $image;
    }
    
    $vars['pizza_builder'] = 0;
    
    
    //VALIDACIÓN DE CAMPOS
    $allUsers = $database->select("SELECT * FROM users WHERE email='". $usuario ."'");
    if($allUsers[0]['email'] != ''){
    } else {
    	$error = 1;
    }

    $allLocal = $database->select("SELECT * FROM config WHERE sitename='". $vars['sitename'] ."'");
    if($allLocal[0]['sitename'] == ''){
    } else {
    	if($error == 1){
    		$error = 3;
    	} else {
    		$error = 2;
    	}
    }	   
    //FIN VALIDACIÓN DE CAMPOS
    
    
    //INSERT NUEVO LOCAL
    if($error != 0){ 	
    	header("Location: //". $urlpath ."/index.php?act=addLocal&error=". $error ." ");
    } else {
    	$database->insert_array('config', $vars);
    
    	$localId = $database->select("SELECT * FROM config WHERE sitename='". $vars['sitename'] ."'");
    	$localId = $localId[0]['id'];
    
    	$rating['uid'] 	= 0;
    	$rating['rid'] 	= $localId;
    	$rating['level']	= 0;
    	$rating['votos']	= 0;
    	$database->insert_array('rating', $rating);
    
    	$options['name']		= 'cb-global-sitedocs';
    	$options['value']	= 1;
    	$options['rid']		= $localId;
    	$database->insert_array('options', $options);
    
    	$options['name']		= $permisosUsuario;
    	$options['value']	= 'news';
    	$database->insert_array('options', $options);
    
    	$options['name']		= $permisosUsuario;
    	$options['value']	= 'locations';
    	$database->insert_array('options', $options);
    
    	$options['name']		= $permisosUsuario;
    	$options['value']	= 'orderstatus';
    	$database->insert_array('options', $options);
    
    	$options['name']		= $permisosUsuario;
    	$options['value']	= 'orders';
    	$database->insert_array('options', $options);
    
    	$options['name']		= $permisosUsuario;
    	$options['value']	= 'homePage';
    	$database->insert_array('options', $options);
    
	    $options['name']		= $permisosUsuario;
    	$options['value']	= 'menu';
    	$database->insert_array('options', $options);
    
    	$options['name']		= $permisosUsuario;
    	$options['value']	= 'restaurant';
    	$database->insert_array('options', $options);
    	
    	
    	//mail
    	$msgBody ='
    	<html>
    		<head>
    			<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
    		</head>
    			
    		<body>
    			<img src="http://www.delivery351.com/images/txtBlack.png">
    			
    			<h3>
    				¡FELICITACIONES! '. $vars['sitename'] .' has sido añadido correctamente a <b>Delivery351</b>
    			</h3>
    				
    				<p>
    					Estimado/a <b>'. $usuario .'</b>
    				
    					<br />
    					<br />
    				
    					Se le han asignado permisos para poder administrar el sitio del local: '. $vars['sitename'] .'
    					<br />
    					Para configurar tu local debes ingresar al panel de administracion 
    					<a href="http://www.delivery351.com/admin" target="_blanck">http://www.delivery351.com/admin</a>
    				
    					<br />
    					<br />
    				    					
    				    Por cualquier inconveniente, conflicto y/o duda no dudes en comunicarte con nosotros.
    			
    					<br />
    					<br />
    					
    					<i>Delivery351</i>
    				</p>
    			</body>
    		</html>
    		';   
    		
    		require_once('configMail.php');
    		
    		$mail->Subject    = "Registro de Local en Delivery351";

    		$mail->MsgHTML($msgBody);
    		
    		$address = $usuario;
    		$mail->AddAddress($address);
    		
    		$mail->Send();
    	
    		header("Location: //". $urlpath ."/index.php?act=localSuccess");
    }      
?>